package dao

import (
	"context"
	"errors"
	"ruoyi-go-echo-ddd/internal/domain/model"
	"ruoyi-go-echo-ddd/internal/infrastructure/persistence/config"

	"github.com/rs/zerolog"
)

type SysUserPostDaoImpl struct {
	db config.PrimaryDB
}

func NewSysUserPostDaoImpl(db config.PrimaryDB) *SysUserPostDaoImpl {
	return &SysUserPostDaoImpl{
		db: db,
	}
}

func (orm *SysUserPostDaoImpl) BatchInsert(ctx context.Context, list []*model.SysUserPost) (int64, error) {
	if len(list) == 0 {
		return 0, errors.New("用户岗位切片不能为空")
	}
	result := orm.db.WithContext(ctx).Model(&model.SysUserPost{}).Create(list)
	if result.Error != nil {
		zerolog.DefaultContextLogger.Error().Err(result.Error).Msg("用户岗位批量新增错误===")
	}
	return result.RowsAffected, nil
}

func (orm *SysUserPostDaoImpl) DeleteByUserId(ctx context.Context, userId int64) (int64, error) {
	var count int64
	if err := orm.db.WithContext(ctx).Where("user_id = ?", userId).Delete(&model.SysUserPost{}).Count(&count).Error; err != nil {
		zerolog.DefaultContextLogger.Error().Err(err).Msg("通过用户ID删除岗位错误===")
	}
	return count, nil
}
